package com.water.camera.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.water.camera.bean.MediaData;
import com.water.camera.databinding.ActivityBatchPreviewBinding;
import com.water.camera.fragment.BaseDialogFragment;
import com.water.camera.fragment.MarkDlg;
import com.water.camera.it.WaterMarkViewChangeListener;
import com.water.camera.mark.BaseMark;
import com.water.camera.util.ToastUtil;
import com.water.camera.util.Utils;

import java.util.ArrayList;

public class BatchPreviewAct extends BaseSaveAct implements WaterMarkViewChangeListener {

    ActivityBatchPreviewBinding binding;
    ArrayList<MediaData> list;
    BaseMark mMark = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBatchPreviewBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.ivClose.setOnClickListener(v -> finish());
        int from = getIntent().getIntExtra("from", 0);
        if (from == 1) {
            String p = getIntent().getStringExtra("path");
            Glide.with(this)
                    .load(p)
                    .into(binding.ivCover);
            binding.btnWaterMask.setVisibility(View.GONE);
            binding.btnSave.setVisibility(View.GONE);
            return;
        }
        list = getIntent().getParcelableArrayListExtra("img");
        Glide.with(this)
//                .setDefaultRequestOptions(RequestOptions.fitCenterTransform())
                .load(list.get(0).getPath())
//                    .override(data.getWidth(), data.getHeight())
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(binding.ivCover);

        binding.btnWaterMask.setOnClickListener(v -> chooseWaterMark());
        binding.btnSave.setOnClickListener(v -> {
            if (mMark == null) {
                ToastUtil.showShortToast(this, "尚未添加水印");
                return;
            }
            save(mMark, binding.waterView, list);
        });
        chooseWaterMark();
    }

    private void chooseWaterMark() {
        MarkDlg dlg = new MarkDlg();
        dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
            @Override
            public void onDismiss(boolean isOk, Object value) {

            }
        });
        dlg.show(getSupportFragmentManager(), "");
    }

    @Override
    protected boolean paddingTop() {
        return false;
    }

    @Override
    public void onWaterMarkViewChange(BaseMark mark) {
        binding.waterView.removeAllViews();
        if (mark != null) {
            mark.setTouchable(true);
            FrameLayout.LayoutParams params;
            if (mark.isFullScreen()) {
                params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            } else if (mark.isFullWidth()) {
                params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.BOTTOM;
                params.bottomMargin = Utils.dp2Px(this, 20);
            } else {
                params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.BOTTOM;
                params.bottomMargin = Utils.dp2Px(this, 20);
                params.leftMargin = params.bottomMargin;
            }
            binding.waterView.addView(mark, params);
        }
        mMark = mark;
    }

    @Override
    public boolean needScale() {
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 102) {//editMarkActivity
            if (resultCode == Activity.RESULT_OK) {
                mMark.update();
                onWaterMarkViewChange(mMark);
            }
        } else if (requestCode == 11) {
            if (resultCode == RESULT_OK) {
                setResult(RESULT_OK);
                finish();
            }
        }
    }

    @Override
    protected void finishSave() {
        Intent i = new Intent();
        i.putExtras(getIntent());
        i.setClass(this, BatchSuccessAct.class);
        i.putStringArrayListExtra("output", output);
        i.putExtra("mark", mMark.getClass().getName());
        i.putExtra("x", mMark.getTranslationX());
        i.putExtra("y", mMark.getTranslationY());
        startActivityForResult(i, 11);
    }
}
